package com.hc.service.impl;

import com.hc.bean.OrderDetail;
import com.hc.bean.OrderInfo;
import com.hc.bean.Pager;
import com.hc.dao.OrderDetailDao;
import com.hc.dao.OrderInfoDao;
import com.hc.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderInfoDao orderInfoDao;
    @Autowired
    private OrderDetailDao orderDetailDao;
    @Override
    public List<OrderInfo> getOrderInfoList(OrderInfo orderInfo, Pager pager) {
        return orderInfoDao.getOrderInfoList(orderInfo,pager);
    }

    @Override
    public int getOrderInfoCount(OrderInfo orderInfo) {
        return orderInfoDao.getOrderInfoCount(orderInfo);
    }

    @Override
    public OrderInfo getOrderInfoById(int id) {
        return orderInfoDao.getOrderInfoById(id);
    }

    @Override
    public List<OrderDetail> getOrderDetailByOid(int oid) {
        return orderDetailDao.getOrderDetailByOrderId(oid);
    }

    @Override
    public Boolean addOrder(OrderInfo orderInfo, List<OrderDetail> orderDetailList) {
        if(orderInfoDao.addOrderInfo(orderInfo)>0){
            for (OrderDetail orderDetail : orderDetailList) {
                orderDetail.setOid(orderInfo.getId());
                if(orderDetailDao.addOrderDetail(orderDetail)<0){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /*删除订单*/
    @Override
    public Boolean deleteOrderInfoByOids(String oids) {
        /*订单明细可能没有,那么删除结果可能为0*/
        if(orderDetailDao.deleteOrderDetailByOid(oids)>=0){
            if(orderInfoDao.deleteOrderInfoByOid(oids)>0){
                return true;
            }
            return false;
        }
        return false;
    }
}
